#ifndef __DEBUG_H
#define __DEBUG_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include "main.h"

#define DEBUG 1
#define SEGGER_RTT 0//要添加jlink的rtt 使用jlink才可以


#if DEBUG

#if SEGGER_RTT
#include "./SEGGER_RTT/SEGGER_RTT.h"
#endif

#if SEGGER_RTT
extern __IO uint32_t uwTick;//stm32 HAL库FREERTOS的tick，用它来加入时间戳
	
	//jlink的SEGGER_RTT_printf不能使用浮点，所以使用浮点用APP_DBG 正常使用DBG
    #define DBG(format,...)  SEGGER_RTT_printf(0, "[%02d:%02d]:"format, (uwTick/1000)/60, (uwTick/1000)%60, ##__VA_ARGS__)
	//使用自带的printf需要自己实现int fputc(int ch, FILE *f)这个函数
    #define APP_DBG(format,...)  printf("[%02d:%02d]:"format, (uwTick/1000)/60, (uwTick/1000)%60, ##__VA_ARGS__)

    #define DBG_R(format,...)  SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_RED"[%02d:%02d]:"format RTT_CTRL_RESET, (uwTick/1000)/60, (uwTick/1000)%60, ##__VA_ARGS__)
    #define DBG_G(format,...)  SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_GREEN"[%02d:%02d]:"format RTT_CTRL_RESET, (uwTick/1000)/60, (uwTick/1000)%60, ##__VA_ARGS__)
    #define DBG_B(format,...)  SEGGER_RTT_printf(0, RTT_CTRL_TEXT_BRIGHT_BLUE"[%02d:%02d]:"format RTT_CTRL_RESET, (uwTick/1000)/60, (uwTick/1000)%60, ##__VA_ARGS__)
#else
    #define DBG(format,...)  printf(format, ##__VA_ARGS__)
    #define APP_DBG(format,...)  printf(format, ##__VA_ARGS__)
#endif
	//打印函数名+行号
    //#define DBG(format,...)  printf("[%s][%05d]:"format, __func__, __LINE__, ##__VA_ARGS__) 
	//打印文件名+打印函数名+行号
    //#define DBG(format,...) printf("["__FILE__"][%s][%05d]: "format, __func__, __LINE__, ##__VA_ARGS__)
	
	//打印16进制
	#define DBG_HEX(buff, len) do{\
								for(int i = 0; i < len; i++)\
								{ printf("%02x ", buff[i]); }\
								printf("\n");\
								}while(0);
	//打印字符
	#define DBG_CHAR(buff, len) do{\
								for(int i = 0; i < len; i++)\
								{ printf("%c", buff[i]); }\
								printf("\n");\
								}while(0);
#else  
    #define DBG(format,...)  
	#define APP_DBG(format,...)
	#define DBG_HEX(buff, len)
#endif 

#ifdef __cplusplus
}
#endif

//Demo
#if 0 //放uart.c里面了
int fputc(int ch, FILE *f)
{
#if SEGGER_RTT
    SEGGER_RTT_PutChar(0, ch);
#else
    while ((USART1->SR & 0X40) == 0);
    USART1->DR = (uint8_t)ch;
#endif
    return ch;
}
#endif

#endif /* __DEBUG_H */
